﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace jqMvcGrid.Models
{
    public class CategoryRepository : BaseDataContext, ICategoryRepository
    {

        #region ICategoryRepository Members

        public bool Create(Category category)
        {
            try
            {
                Northwind.Categories.InsertOnSubmit(category);
                Northwind.SubmitChanges();
                return true;
            }
            catch { return false; }
        }

        public Category Details(int categoryId)
        {
            try
            {
                return Northwind.Categories.Single(p => p.CategoryID == categoryId);
            }
            catch { return null; }
        }

        public bool Edit(Category category)
        {
            try
            {
                var entity = Details(category.CategoryID);
                entity.CategoryName = category.CategoryName;
                entity.Description = category.Description;
                //entity.Picture = category.Picture;
                Northwind.SubmitChanges();
                return true;
            }
            catch { return false; }
        }

        public bool Delete(int categoryId)
        {
            try
            {
                Northwind.Categories.DeleteOnSubmit(Details(categoryId));
                return true;
            }
            catch { return false; }
        }

        public IQueryable<Category> Index()
        {
            try
            {
                return Northwind.Categories.AsQueryable();
            }
            catch { return null; }
        }

        #endregion
    }

    public interface ICategoryRepository
    {
        bool Create(Category category);
        Category Details(int categoryId);
        bool Edit(Category category);
        bool Delete(int categoryId);
        IQueryable<Category> Index();
    }
}
